<?php
/**
 * 不同模块设置异常处理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/11/16
 * Time: 15:27
 */
namespace app\api\exception;
use think\exception\Handle;
use think\Response;
use Throwable;

class Http  extends Handle{
    public $httpStatus = 500;
    /**
     * Render an exception into an HTTP response.
     *
     * @access public
     * @param \think\Request   $request
     * @param Throwable $e
     * @return Response
     */
    public function render($request, Throwable $e): Response
    {
        if($e instanceof  \think\Exception){//设置抛出异常返回数据
            return show($e->getCode(),$e->getMessage());
        }
        if($e instanceof \think\exception\HttpResponseException){
            return parent::render($request,$e);
        }
        if(method_exists($e,'getStatusCode')){//判断这个对象是否有此方法 设置返回状态码
            $httpStatus = $e->getStatusCode();
        }else{
            $httpStatus = $this->httpStatus;
        }
        // 添加自定义异常处理机制
        return show(config('status.error'),$e->getMessage(),[],$httpStatus);

    }
}